<%@page import="com.keta.foundation.entity.RolePermission"%>
<%@page import="com.keta.foundation.entity.Role"%>
<%@page import="com.keta.foundation.entity.Permission"%>
<%@page import="com.keta.foundation.entity.Module"%>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%@ include file="/WEB-INF/views/include.inc.jsp"%>
<%!
	public int INDEX =0;
	public boolean isExistPermission(Role role,Permission permission){
		if(role.getRolePermissions()!=null){
			
			for(RolePermission rolePermission : role.getRolePermissions()){
				if(rolePermission.getPermission().getId()==permission.getId()){
					return true;
				}
			}
		}else{
			return false;
		}
		
		return false;
	}
	
	public String getRolePermissionId(Role role,Permission permssion){
		for(RolePermission rolePermission : role.getRolePermissions()){
			if(rolePermission.getPermission().getId()==permssion.getId() &&
					rolePermission.getRole().getId()==role.getId()){
				return rolePermission.getId().toString();
			}
		}
		return "";
	}
	
	public String module_row(Module module,Role role){
	 	StringBuilder sb = new StringBuilder();
	 	if(module.getParent()!=null){
	 		sb.append("<tr id=\"" + module.getId()+ "\" pid=\""+module.getParent().getId()+"\">");
	 	}else{
	 		sb.append("<tr id=\"" + module.getId()+ "\" >");
	 	}
	 	sb.append("<td><span ref=\"treeChk\" class=\"button chk checkbox_false_full\" /><span ref=\"allChk\" class=\"button chk checkbox_false_full setAll\"/>"+module.getName()+"</td>");
	 	sb.append("<td><span class=\"inputValueRole\">");
	 	
	 	for(Permission permission : module.getPermissions()){
	 		sb.append("<input type=\"hidden\" name=\"rolePermissions["+INDEX+"].id\" value=\""+getRolePermissionId(role,permission)+"\"/>");
	 		if(isExistPermission(role,permission)){
	 			sb.append(permission.getName() + "<input type=\"checkbox\" checked=\"checked\" name=\"rolePermissions["+INDEX+"].permission.id\" value=\""+permission.getId()+"\"/>");
	 		}else{
	 			sb.append(permission.getName() + "<input type=\"checkbox\" name=\"rolePermissions["+INDEX+"].permission.id\" value=\""+permission.getId()+"\"/>");
	 		}
	 		INDEX++;
	 	}
	 	
	 	sb.append("</span></td>");
	 	sb.append("</tr>");
		return sb.toString();
	}
	
	public String role_tree(Module module,Role role){
		StringBuilder sb = new StringBuilder();
		sb.append(module_row(module,role));
		if(module.getChildren()!=null){
			for(Module subModule :module.getChildren()){
				sb.append(role_tree(subModule,role));
			}
		}
		return sb.toString();
	}
%>
<% 
	Module rootModule = (Module)request.getAttribute("rootModule");
	Role role = (Role)request.getAttribute("role");
	INDEX = 0;
	String role_tree = role_tree(rootModule,role);
%>

<script type="text/javascript">

$(document).ready(function(){
	initRolePage();
	// 初始化全选状态
	$(".setAll").each(function(){
    	var $td = $(this).parent().nextAll("td");
		var $inputSpan = $(".inputValueRole", $td);
		var allCheckBoxLength = $("input[type=checkbox]", $inputSpan).length;
		var checkedLength = $("input:checked", $inputSpan).length; 
		if (allCheckBoxLength == checkedLength) {
			$(this).attr("class", "button chk checkbox_true_full setAll");
		}
	});
});


</script>

<div class="pageContent">
	<form method="post" action="${contextPath}/role/edit" class="required-validate pageForm" onsubmit="return validateCallback(this, navTabReloadParent);">
		<div class="pageFormContent" layoutH="56">
			<input type="hidden" name="id" value="${role.id}" />
			<dl>
				<dt>名称：</dt>
				<dd>
					<input type="text" name="name" class="input-medium validate[required]"
						value="${role.name}"/>
				</dd>
			</dl>
			<dl>
				<dt>角色描述：</dt>
				<dd>
					<input type="text" style="width:400px;" name="description" class="input-medium"
						value="${role.description}"  />
				</dd>
			</dl>
			<div class="divider"></div>
			<table class="treeTable list" width="100%">
				<tr>
					<td>模块名称</td>
					<td width="70%">操作权限</td>
				</tr>
				
				
				<%=role_tree %>
   
	

			</table>
		</div>

		<div class="formBar">
		<ul>
			<li><div class="button"><div class="buttonContent"><button type="submit">确定</button></div></div></li>
			<li><div class="button"><div class="buttonContent"><button class="close" type="button">关闭</button></div></div></li>
		</ul>
		</div>
	</form>
</div>